package com.xms.ribbonbutton.api.controller;

import com.xms.core.exception.XmsException;
import com.xms.ribbonbutton.api.model.UpdateRibbonButtonModel;
import com.xms.ribbonbutton.core.RibbonButtonArea;
import com.xms.ribbonbutton.entity.RibbonButton;
import com.xms.ribbonbutton.service.IRibbonButtonFinderService;
import com.xms.ribbonbutton.service.IRibbonButtonUpdaterService;
import com.xms.utils.CollectionUtil;
import com.xms.web.framework.controller.ApiBaseController;
import com.xms.web.framework.model.JsonResultObject;
import com.xms.web.framework.model.SetRecordStateModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;

/**
 * <p>
 * RibbonButtonUpdaterController
 * </p>
 *
 * @author migo
 * @since 2021/9/17
 */
@RestController
@RequestMapping("{org}/api/ribbonbutton")
public class RibbonButtonUpdaterController extends ApiBaseController {
    private IRibbonButtonUpdaterService _ribbonButtonUpdaterService;
    private IRibbonButtonFinderService _ribbonButtonFinderService;

    @Autowired
    public void RibbonButtonUpdaterController(IRibbonButtonUpdaterService ribbonButtonUpdaterService,
                                              IRibbonButtonFinderService ribbonButtonFinderService) {
        _ribbonButtonUpdaterService = ribbonButtonUpdaterService;
        _ribbonButtonFinderService = ribbonButtonFinderService;
    }

    @ApiOperation("修改按钮")
    @PostMapping("update")
    public JsonResultObject update(@RequestBody UpdateRibbonButtonModel model) throws XmsException, SQLException {
        RibbonButton entity = _ribbonButtonFinderService.getById(model.ribbonbuttonid);
        if (entity == null) {
            return JNotFound();
        }
        entity.setCommandrules(model.commandrules);
        entity.setShowlabel(model.showlabel);
        entity.setShowarea(model.showarea);
        entity.setJslibrary(model.jslibrary);
        entity.setJsaction(model.jsaction);
        entity.setDisplayorder(model.displayorder);
        entity.setCssclass(model.cssclass);
        entity.setLabel(model.label);
        entity.setShowlabel(model.showlabel);
        entity.setIcon(model.icon);
        entity.setAuthorizationenabled(model.authorizationenabled);
        entity.setDescription(model.description);
        if (entity.getShowarea() != RibbonButtonArea.Form && entity.getShowarea() != RibbonButtonArea.ListRow) {
            entity.setCommandrules("");
        }
        return _ribbonButtonUpdaterService.update(entity) ? JUpdateSuccess() : JUpdateFailure();
    }

    @ApiOperation("设置按钮可用状态")
    @PostMapping("setstate")
    public JsonResultObject setState(@RequestBody SetRecordStateModel model) {
        if (CollectionUtil.isEmpty(model.recordid)) {
            return JNotSpecifiedParams("id");
        }
        if (_ribbonButtonUpdaterService.updateState(model.isenabled, model.recordid)) {
            return JUpdateSuccess();
        }
        return JUpdateFailure();
    }
}
